<template>
  <div>
    <el-dialog :title="pid ? '添加权限点':'编辑权限点'" :visible="dialogFormVisible" @close="onClose">
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code" :label-width="formLabelWidth">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
          inactive-text="开启"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onClick">确 定</el-button>
        <el-button @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSysPermissionId, postSysPermission, putSysPermission } from '@/api/permission/permission'
export default {
  name: 'PermissionDialog',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    // 添加的子集pid
    pid: {
      type: [Number, String],
      default: null
    },
    // 编辑的id值
    id: {
      type: [Number, String],
      default: null
    }
  },
  data() {
    return {
      formLabelWidth: '120px',
      value: true,
      form: {
        name: '',
        code: '',
        description: '',
        type: 1,
        enVisible: '1'
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    id: {
      async handler(newValue) {
        if (newValue) {
          this.form = await getSysPermissionId(newValue)
        }
      }
    }
  },
  methods: {
    onClose() {
      this.$emit('update:pid', '')
      this.$emit('update:id', '')
      this.$emit('update:dialogFormVisible', false)
      this.form = {
        name: '',
        code: '',
        description: '',
        type: 2,
        enVisible: '1'
      }
    },
    async onClick() {
      if (this.pid) {
        // 添加
        try {
          await postSysPermission({ ...this.form, pid: this.pid })
          this.$emit('updatePermissionDialog')
          this.onClose()
          this.$message.success('添加成功')
        } catch (error) {
          console.log(error)
        }
      } else {
        try {
          await putSysPermission({ ...this.form, id: this.id })
          this.$emit('updatePermissionDialog')
          this.onClose()
          this.$message.success('修改成功')
        } catch (error) {
          console.log(error)
        }
      }
    }
  }
  // 编辑
  // async echo() {
  //   this.form = await getSysPermissionId(this.id)
  //   console.log(this.form)
  // }
}
</script>

<style lang="scss" scoped>
.el-switch {
  margin-left: 70px;
}
</style>
